%% humans
% listMeasName = {
%     '191108_Bob_HumanBlood1'
%     '191113_Bob_HumanBlood2'
%     '201118_DoreenBob_HumanBlood3'
%     '201125_DoreenBob_HumanBlood4'
%     '201126_DoreenBob_HumanBlood5'
%     };
% strSearchAnimal = 'H';
% strAnimalType = 'Human';
% targetName = 'HumanTable';
% flagWildBatPara = 0;

%% bats 1. round
% listMeasName = {
%     '191015_GeraldDoreenBob_Bats1'
%     '191016_GeraldDoreenBob_Bats2'
%     '191030_GeraldDoreenBob_Bats3'
%     '201008_GeraldDoreenBob_Bats4'
%     '201021_GeraldDoreenBob_Bats5'
%     '201028_GeraldDoreenBob_Bats6'
%     };
% strSearchAnimal = 'Bat';
% strAnimalType = 'NyctalusNoctula';
% targetName = 'Bat1Table';
% flagWildBatPara = 1;

%% bats 2. round
% listMeasName = {
%     '210408_FaruqBob_Bats21'
%     '210518_Faruq_1Wildbat'
%     '210520_Faruq_1Wildbat'
%     '210527_Faruq_1Wildbat'
%     '210809_Faruq_2Wildbats'
%     '210811_Faruq_2_Wildbats'
%     '210819_Faruq_2Wildbats'
%     '210825_Faruq_1Wildbat'
%     '210916_Faruq_1Wildbat'
%     '210923_Faruq_2Wildbats'
%     '210929_Faruq_2Wildbats'
%     '211012_Faruq_2wildbats'
%     '211019_Faruq_2Wildbats'
%     '211026_Faruq_2Wildbats'
%     '211029_Faruq_2wildbats'
%     };
% strSearchAnimal = 'Bat';
% strAnimalType = 'NyctalusNoctula';
% targetName = 'Bat2Table';
% flagWildBatPara = 1;

%% FLI bats
listMeasName = {
    '210812_Faruq_2_FLIbats'
    '210902_Faruq_2FLIbats'
    '210917_Faruq_2FLIBats'
    '210924_Faruq_2FLIBats'
    '211001_Faruq_2FLIbats'
    '211007_Faruq_2FLIBats'
    };
strSearchAnimal = 'Bat';
strAnimalType = 'RousettusAegyptiacus';
targetName = 'BatFLITable';
flagWildBatPara = 0;

%% para
tempLevels = {'Cold','RT','Warm'};

opt = detectImportOptions('20220301_summary.xlsx','Sheet',strAnimalType);
TAnimalPara = readtable('20220301_summary.xlsx',opt);

%% script: combine csv tables
TAll = table;
% fprintf([repmat('-',1,length(listMeasName)) '\n']);
for idxListMeasName = 1:length(listMeasName)
    measDir = ['..' filesep listMeasName{idxListMeasName} filesep];
    fprintf('%s\n', measDir);
    FTDir = [measDir filesep 'FTFitData' filesep];
    
    idx = find(listMeasName{idxListMeasName} == '_');
    date = listMeasName{idxListMeasName}(1:idx(1)-1);
    TMaskDate = ismember(TAnimalPara.DateCode,str2double(date));
    
    csvFiles = dir([FTDir listMeasName{idxListMeasName} '__' '*' '.csv']);
    for idxCSVFile = 1:length(csvFiles)
        T = readtable([csvFiles(idxCSVFile).folder filesep csvFiles(idxCSVFile).name]);
        
        idx = strfind(csvFiles(idxCSVFile).name,'__');
        idx1 = strfind(csvFiles(idxCSVFile).name(idx+2:end),strSearchAnimal);
        if ~isempty(idx1)
            idx2 = strfind(csvFiles(idxCSVFile).name(idx+2+idx1-1:end),'_');
            strAnimalIdentifier = csvFiles(idxCSVFile).name(idx+2+idx1-1+(0:idx2(1)-1-1));
        else
            strAnimalIdentifier = '';
        end
        TMaskRow = TMaskDate & ismember(TAnimalPara.Animal,strAnimalIdentifier);
        maskTempLevel = [contains(csvFiles(idxCSVFile).name,tempLevels{1}), contains(csvFiles(idxCSVFile).name,tempLevels{2}), contains(csvFiles(idxCSVFile).name,tempLevels{3})];
        
        TExtend = table;
        TExtend.dateCode = repmat(TAnimalPara.DateCode(TMaskRow),height(T),1);
        TExtend.year  = repmat(TAnimalPara.Year(TMaskRow),height(T),1);
        TExtend.month = repmat(TAnimalPara.Month(TMaskRow),height(T),1);
        TExtend.day   = repmat(TAnimalPara.Day(TMaskRow),height(T),1);
        TExtend.animalType = repmat({strAnimalType},height(T),1);
        TExtend.index = repmat(TAnimalPara.Index(TMaskRow),height(T),1);
        TExtend.tempLevel = repmat(tempLevels(maskTempLevel),height(T),1);
        TExtend.sex = repmat(TAnimalPara.Sex(TMaskRow),height(T),1);
        if flagWildBatPara
            TExtend.bodyMass = repmat(TAnimalPara.BodyMass_g(TMaskRow),height(T),1);
            TExtend.forearmLength = repmat(TAnimalPara.ForearmLength_mm(TMaskRow),height(T),1);
            TExtend.tempAtCapture = repmat(TAnimalPara.TempAtCapture_degC(TMaskRow),height(T),1);
            TExtend.torporCapture = repmat(TAnimalPara.TorporCapture(TMaskRow),height(T),1);
            TExtend.torporBlood = repmat(TAnimalPara.TorporBlood(TMaskRow),height(T),1);
        end
        TExtend.operator = repmat(TAnimalPara.Operator(TMaskRow),height(T),1);
        
        TAll = [TAll; [TExtend T]];
    end
%     fprintf('-');
end
% fprintf('\n');

writetable(TAll, [targetName '.csv']);

